package com.aefottt.struggle_star.view;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.aefottt.struggle_star.R;

public class NotifyToast extends Toast {
    private static NotifyToast notifyToast;
    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    private NotifyToast(Context context) {
        super(context);
    }

    public static NotifyToast getInstance(Context context){
        if (notifyToast == null){
            synchronized (NotifyToast.class){
                if (notifyToast == null){
                    notifyToast = new NotifyToast(context);
                }
            }
        }
        return notifyToast;
    }

    public static class Builder{
        private NotifyToast toast;
        private View layout;
        private Button btn;
        private TextView tv;

        public Builder(Context context) {
            toast = getInstance(context);
            layout = LayoutInflater.from(context).inflate(R.layout.toast_notify, null, false);
            btn = layout.findViewById(R.id.btn_toast_notify);
            tv = layout.findViewById(R.id.tv_toast_notify);
        }

        public Builder setContent(String content){
            tv.setText(content);
            return this;
        }

        public Builder setBtnText(String text){
            btn.setText(text);
            return this;
        }

        public Builder setBtnClickListener(OnToastClickListener listener){
            if (listener != null){
                btn.setOnClickListener(v -> {
                    toast.cancel();
                    listener.onClick(toast);
                });
            }
            return this;
        }

        public void showToast(int gravity){
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.setGravity(gravity, 0, 0);
            toast.show();
        }
    }

    public interface OnToastClickListener {
        void onClick(NotifyToast toast);
    }
}
